import com.jiawei.message.decode.StringProtocol;
import org.smartboot.socket.MessageProcessor;
import org.smartboot.socket.StateMachineEnum;
import org.smartboot.socket.transport.AioQuickServer;
import org.smartboot.socket.transport.AioSession;

import java.io.IOException;

/**
 * @author : willian fu
 * @version : 1.0
 */
public class Server {
    public static void main(String[] args) throws IOException {
        // 1
        AioQuickServer<String> server = new AioQuickServer<String>(1995, new StringProtocol(), new MessageProcessor<String>() {
            public void process(AioSession<String> session, String msg) {
                System.out.println("接受到客户端消息:" + msg);

                byte[] response = "Hi Client!".getBytes();
                byte[] head = {(byte) response.length};
                try {
                    session.writeBuffer().write(head);
                    session.writeBuffer().write(response);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            public void stateEvent(AioSession<String> session,
                                   StateMachineEnum stateMachineEnum,
                                   Throwable throwable) {
                switch (stateMachineEnum){
                    case NEW_SESSION:
                        try {
                            System.out.println("与客户端连接建立"+session.getRemoteAddress());
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        break;
                    case REJECT_ACCEPT:break;
                    case INPUT_SHUTDOWN:break;
                    case SESSION_CLOSED:break;
                    case DECODE_EXCEPTION:break;
                    case INPUT_EXCEPTION:break;
                    case SESSION_CLOSING:break;
                    case OUTPUT_EXCEPTION:break;
                    case PROCESS_EXCEPTION:break;
                }
            }
        });
        //2
        server.start();
    }
}
